package com.saifan.wyy_ov.ui.onlishop;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.data.bean.CartBean;
import com.saifan.wyy_ov.data.bean.StoreBean;
import com.saifan.wyy_ov.data.bean.CartBean.GoodsList;
import com.saifan.wyy_ov.utils.ImgUtil;
import com.saifan.wyy_ov.utils.PriceUtils;
import com.saifan.wyy_ov.utils.StringUtil;

public class AdapterCart extends BaseAdapter {
    Context context;
    List<CartBean> cartBeans;
    Handler handler;
    List<AdapterCartItem> adapterCartItems = new ArrayList<AdapterCartItem>();

    public List<CartBean> getCartBeans() {
        return cartBeans;
    }

    public void setCartBeans(List<CartBean> cartBeans) {
        this.cartBeans = cartBeans;
    }

    public AdapterCart(Context context, List<CartBean> cartBeans, Handler handler) {
        // TODO Auto-generated constructor stub
        this.context = context;
        this.cartBeans = cartBeans;
        this.handler = handler;
    }

    @Override
    public void notifyDataSetChanged() {
        // TODO Auto-generated method stub
        super.notifyDataSetChanged();
        for (AdapterCartItem adapterCartItem : adapterCartItems) {
            adapterCartItem.notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if (cartBeans!=null && !cartBeans.isEmpty())
            return cartBeans.size();
        else
            return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        if (cartBeans!=null && !cartBeans.isEmpty())
            return cartBeans.get(position);
        else
            return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder viewHolder = null;
        List<GoodsList> goodsLists = cartBeans.get(position).getGoodsList();
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.cart_view, null);
            bindViews(viewHolder, convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        double total = 0;
        int count = 0;
        if (goodsLists!=null&&!goodsLists.isEmpty()) {
            for (GoodsList goodsList : goodsLists) {
                total += (goodsList.getGoodsPrice() * goodsList.getCount());
                count += goodsList.getCount();
            }
        }

        boolean isAllpay = true;
        for (GoodsList goodsList : goodsLists) {
            if (!goodsList.isIspay()) {
                isAllpay = false;
                break;
            }
        }
        final AdapterCartItem adapterOrderItem = new AdapterCartItem(context, cartBeans.get(position), handler);
        adapterCartItems.add(adapterOrderItem);
        viewHolder.lv_cartDetail.setAdapter(adapterOrderItem);
        viewHolder.ll_store.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View paramView) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(context, StoreDetailsActivity.class);
                StoreBean storeBean = new StoreBean();
                storeBean.setID(cartBeans.get(position).getStoreID());
                storeBean.setStoreName(cartBeans.get(position).getStoreName());
                storeBean.setHeadPhoto(cartBeans.get(position).getHeadPhoto());
                storeBean.setTel(cartBeans.get(position).getTel());
                storeBean.setBusinessDate(cartBeans.get(position).getBusinessDate());
                storeBean.setBusinessTime(cartBeans.get(position).getBusinessTime());
                storeBean.setAddress(cartBeans.get(position).getAddress());
                storeBean.setIsDelivery(true);
                intent.putExtra("data", storeBean);
                context.startActivity(intent);
            }
        });

        ImgUtil.DisPlayImg(cartBeans.get(position).getHeadPhoto(), viewHolder.iv_store_img, context);
        viewHolder.tv_store_name.setText(StringUtil.returnShow(cartBeans.get(position).getStoreName()) + " ");
        viewHolder.cb_all.setChecked(isAllpay);
        viewHolder.cb_all.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // TODO Auto-generated method stub
                Message message = new Message();
                message.what = 12;
                message.obj = arg1;
                handler.sendMessage(message);
                for (GoodsList goodsList : cartBeans.get(position).getGoodsList()) {
                    goodsList.setIspay(arg1);
                }
                adapterOrderItem.notifyDataSetChanged();
            }
        });
        viewHolder.tv_count.setText(count + "");
        viewHolder.tv_total.setText(PriceUtils.getPrice(total) + "元");

        return convertView;
    }

    private void bindViews(ViewHolder viewHolder, View convertView) {
        viewHolder.iv_store_img = (com.saifan.wyy_ov.ui.view.CircleImageView) convertView.findViewById(R.id.iv_store_img);
        viewHolder.tv_store_name = (TextView) convertView.findViewById(R.id.tv_store_name);
        viewHolder.cb_all = (CheckBox) convertView.findViewById(R.id.cb_all);
        viewHolder.lv_cartDetail = (com.saifan.wyy_ov.ui.view.NoScrollListView) convertView.findViewById(R.id.lv_cartDetail);
        viewHolder.tv_count = (TextView) convertView.findViewById(R.id.tv_count);
        viewHolder.tv_total = (TextView) convertView.findViewById(R.id.tv_total);
        viewHolder.ll_store = (LinearLayout) convertView.findViewById(R.id.ll_store);

    }

    static class ViewHolder {
        private com.saifan.wyy_ov.ui.view.CircleImageView iv_store_img;
        private TextView tv_store_name;
        private CheckBox cb_all;
        private com.saifan.wyy_ov.ui.view.NoScrollListView lv_cartDetail;
        private TextView tv_count;
        private TextView tv_total;
        LinearLayout ll_store;
    }
}
